<?php


namespace api\admin\controller;


use api\admin\model\DeliveryModel;
use api\admin\validate\DeliveryValidate;
use cmf\controller\RestBaseController;
use think\Request;

class DeliveryController extends RestBaseController
{
    //查询内容，搜索名称
    public function getList(){
        //分页
        $page = input('page/d',1);
        $pageSize = input('page_size/d',10);
        $list = DeliveryModel::order('id','desc')
            ->field('id,consignee,mobile,area,address')
            ->page($page,$pageSize)
            ->select();
        $this->success('收货信息列表',$list);
    }
    //添加
    public function add(Request $request){
        $data = input();
        $data['user_id'] = $request->user_id;
        //验证数据
        $validate = new DeliveryValidate();
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $add = DeliveryModel::create($data);
        if($add){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }
    //修改更新数据
    public function edit(Request $request){
        $data = input();
        $id = $request->user_id;
        //验证数据
        $validate = new DeliveryValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $edit = DeliveryModel::where('id',$id)->update($data);
        if($edit){
            $this->success('保存成功');
        }
        $this->error('保存失败');
    }
    //删除地址
    public function delete(Request $request){
        $data = input('get.');
        $data['id'] =  $request->user_id;
        if(!$data['id']){
            $this->error('参数错误');
        }
        if(DeliveryModel::destroy($data['id'])){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}